package cst.a931xmc.yuejian20.Activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.squareup.picasso.Picasso;

import cst.a931xmc.yuejian20.Adapter.ActivityAdapter;
import cst.a931xmc.yuejian20.Bean.ActivityBean;
import cst.a931xmc.yuejian20.Listener.Listener;
import cst.a931xmc.yuejian20.Model.ActivityModel;
import cst.a931xmc.yuejian20.Model.ApiModel;
import cst.a931xmc.yuejian20.R;

public class ActivityActivity extends AppCompatActivity implements View.OnClickListener {
    private Context context;
    private int activity_id;
    private ImageView activity_pic;
    private TextView activity_name;
    private TextView activity_place;
    private TextView activity_time;
    private TextView activity_tel;
    private TextView activity_appnum;
    private TextView activity_price;
    private TextView activity_intro;
    private ImageButton btn_back;
    private Button btn_app;
    private ActivityModel model;
    private Listener<ActivityBean> listener;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_activity);
        init();
        initData();
        model = new ActivityModel();
        model.getDetail(activity_id,listener);
    }

    public void init(){
        activity_name = findViewById(R.id.activity_name);
        activity_pic = findViewById(R.id.activity_pic);
        activity_place = findViewById(R.id.activity_place);
        activity_time = findViewById(R.id.activity_time);
        activity_tel = findViewById(R.id.activity_tel);
        activity_appnum = findViewById(R.id.activity_appnum);
        activity_price = findViewById(R.id.activity_price);
        activity_intro = findViewById(R.id.activity_intro);
        btn_back = findViewById(R.id.btn_back);
        btn_app = findViewById(R.id.btn_app);
        btn_back.setOnClickListener(this);
        btn_app.setOnClickListener(this);
        Intent intent = getIntent();
        activity_id = intent.getExtras().getInt("activity_id");
    }

    public void initData(){
        listener = new Listener<ActivityBean>() {
            @Override
            public void onResponse(ActivityBean l) {
                if(l.getActivity_id()!=0) {
                    activity_name.setText(l.getActivity_name());
                    activity_place.setText("地点：" + l.getActivity_place());
                    activity_time.setText("时间：" + l.getActivity_time());
                    activity_tel.setText("咨询电话：" + l.getActivity_tel());
                    activity_appnum.setText(l.getActivity_appnum());
                    activity_price.setText("￥" + l.getActivity_price() + "/人次");
                    activity_intro.setText(l.getActivity_intro());
                    ApiModel apiModel = new ApiModel();
                    Picasso.with(context)
                            .load(apiModel.getBaseUrl() + "static/images/" + l.getActivity_pic())
                            .placeholder(R.drawable.nopic)
                            .into(activity_pic);
                }
                else {
                    Toast.makeText(ActivityActivity.this,"加载失败",Toast.LENGTH_LONG).show();
                }
            }

            @Override
            public void onFail(String message) {
                Toast.makeText(ActivityActivity.this,message, Toast.LENGTH_LONG).show();
            }
        };
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_back:
                finish();
                break;
            case R.id.btn_app:
                Intent intent = new Intent(ActivityActivity.this, ActivityAppActivity.class);
                intent.putExtra("activity_id", activity_id);
                startActivity(intent);
                finish();
                break;
        }
    }
}
